package com.ruoyi.user.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.manage.domain.Donation;
import com.ruoyi.manage.domain.GoodsShelves;
import com.ruoyi.manage.domain.User;
import com.ruoyi.manage.domain.UserDonation;
import com.ruoyi.manage.domain.vo.DonationVo;
import com.ruoyi.manage.service.IDonationService;
import com.ruoyi.manage.service.impl.UserDonationServiceImpl;
import com.ruoyi.user.utils.MailUtil;
import com.ruoyi.user.utils.javabean.Mail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.input.buffer.PeekableInputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZhangZhongGen
 */
@Slf4j
@RestController
@Api(tags = "用户端捐赠模块")
@RequestMapping("/userWeixin/donation")
public class UserDonationController extends BaseController {
    @Autowired
    private IDonationService donationService;
    @Autowired
    private MailUtil  mailUtil;
    @Autowired
    private UserDonationServiceImpl userDonationService;

    @GetMapping("/list/{schoolId}")
    @ApiOperation("查询捐赠列表")
    public TableDataInfo list(Donation donation,@PathVariable("schoolId") Long schoolId) {
        //先排除种类id为null的情况,避免与0比较时,底层调用的Long.valueOf(null)报错
        if(donation.getCategoryId()==null||donation.getCategoryId()==0)donation.setCategoryId(null);//如果种类id为0表示查询全部
        System.out.println("学校id:"+schoolId);
        return getDataTable(donationService.selectDonationVoListBySchoolId(donation,schoolId));
    }

    /**
     * 获取捐赠物品详细信息
     */
    @ApiOperation("获取捐赠物品详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        DonationVo donationVo = donationService.selectDonationVoById(id);
        int i = userDonationService.selectUserDonationByUD(UserDonation.builder().userId(getUser().getId()).donationId(id).build());
        donationVo.setIsCollect(i);
        return success(donationVo);
    }

    /**
     * 新增捐赠物品
     */
    @ApiOperation("新增捐赠物品")
    @PostMapping
    public AjaxResult add(@RequestBody Donation donation)
    {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        donation.setDonorId(principal.getId());
        int i = donationService.insertDonation(donation);
        if(i==1){//发送邮件-通知管理员审核
            mailUtil.sendSimpleMail(new Mail("3099729178@qq.com","爱心小屋","有新的捐赠订单,请尽快审核"));
        }
        return toAjax(i);
    }

    /**
     * 把捐赠物添加到用户收藏夹
     * @param donationId
     * @return
     */
    @ApiOperation("把捐赠物添加到用户收藏夹")
    @PostMapping("/addCollection/{id}")
    public AjaxResult addCollection(@PathVariable("id") Long donationId) {
        UserDonation ud = UserDonation.builder().donationId(donationId).userId(getUser().getId()).build();
        return success(userDonationService.insertUerDonation(ud));
    }

    /**
     * 捐赠物-取货或捐赠
     * @param type
     * @param goodsShelves
     * @return
     */
    @ApiOperation("捐赠物-取货或捐赠")
    @PutMapping("/storageOrPickup/{type}")
    public AjaxResult storageOrPickup(@PathVariable("type") String type, @RequestBody GoodsShelves goodsShelves){
        int n= userDonationService.storageOrPickup(type,goodsShelves,getUser().getId());
        if (n==0){
            return error("操作失败");
        }
        return success(n);
    }



    /**
     * 获取当前微信用户
     * @return
     */
    public User getUser(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User principal = (User) authentication.getPrincipal();
        return principal;
    }
}
